
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>7. 同步互斥 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part8.html" />
    
    
    <link rel="prev" href="part6.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter0/introduction.html">
            
                <a href="../chapter0/introduction.html">
            
                    
                    第零章：实验环境说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter1/introduction.html">
            
                <a href="../chapter1/introduction.html">
            
                    
                    第一章：独立式可执行程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter1/part1.html">
            
                <a href="../chapter1/part1.html">
            
                    
                    [安装 nightly rust]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter1/part2.html">
            
                <a href="../chapter1/part2.html">
            
                    
                    使用包管理器 cargo 创建 rust binary 项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter1/part3.html">
            
                <a href="../chapter1/part3.html">
            
                    
                    移除标准库依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter1/part4.html">
            
                <a href="../chapter1/part4.html">
            
                    
                    [移除 runtime 依赖]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter1/part5.html">
            
                <a href="../chapter1/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter2/introduction.html">
            
                <a href="../chapter2/introduction.html">
            
                    
                    第二章：最小化内核
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter2/part1.html">
            
                <a href="../chapter2/part1.html">
            
                    
                    使用目标三元组描述目标平台
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter2/part2.html">
            
                <a href="../chapter2/part2.html">
            
                    
                    编译、生成内核镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter2/part3.html">
            
                <a href="../chapter2/part3.html">
            
                    
                    使用链接脚本指定内存布局
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter2/part4.html">
            
                <a href="../chapter2/part4.html">
            
                    
                    [重写程序入口点 -start]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter2/part5.html">
            
                <a href="../chapter2/part5.html">
            
                    
                    [使用 Qemu 运行内核]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter2/part6.html">
            
                <a href="../chapter2/part6.html">
            
                    
                    封装 SBI 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter2/part7.html">
            
                <a href="../chapter2/part7.html">
            
                    
                    [实现格式化输出]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter2/part8.html">
            
                <a href="../chapter2/part8.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter3/introduction.html">
            
                <a href="../chapter3/introduction.html">
            
                    
                    第三章：中断
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter3/part1.html">
            
                <a href="../chapter3/part1.html">
            
                    
                    rv64 中断介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter3/part2.html">
            
                <a href="../chapter3/part2.html">
            
                    
                    [手动触发断点中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter3/part3.html">
            
                <a href="../chapter3/part3.html">
            
                    
                    程序运行上下文环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../chapter3/part4.html">
            
                <a href="../chapter3/part4.html">
            
                    
                    [实现上下文环境保存与恢复]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../chapter3/part5.html">
            
                <a href="../chapter3/part5.html">
            
                    
                    [时钟中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../chapter3/part6.html">
            
                <a href="../chapter3/part6.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter4/introduction.html">
            
                <a href="../chapter4/introduction.html">
            
                    
                    第四章：内存管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter4/part1.html">
            
                <a href="../chapter4/part1.html">
            
                    
                    [物理内存探测与管理]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter4/part2.html">
            
                <a href="../chapter4/part2.html">
            
                    
                    [动态内存分配]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter4/part3.html">
            
                <a href="../chapter4/part3.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter5/introduction.html">
            
                <a href="../chapter5/introduction.html">
            
                    
                    第五章：内存虚拟化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/part1.html">
            
                <a href="../chapter5/part1.html">
            
                    
                    页表：从虚拟内存到物理内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter5/part2.html">
            
                <a href="../chapter5/part2.html">
            
                    
                    [内核初始映射]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter5/part3.html">
            
                <a href="../chapter5/part3.html">
            
                    
                    内核重映射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter5/part4.html">
            
                <a href="../chapter5/part4.html">
            
                    
                    内核重映射实现之一：页表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter5/part5.html">
            
                <a href="../chapter5/part5.html">
            
                    
                    内核重映射实现之二：MemorySet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../chapter5/part6.html">
            
                <a href="../chapter5/part6.html">
            
                    
                    [内核重映射实现之三：完结]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../chapter5/part7.html">
            
                <a href="../chapter5/part7.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter6/introduction.html">
            
                <a href="../chapter6/introduction.html">
            
                    
                    第六章：内核线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter6/part1.html">
            
                <a href="../chapter6/part1.html">
            
                    
                    线程状态与保存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter6/part2.html">
            
                <a href="../chapter6/part2.html">
            
                    
                    线程切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter6/part3.html">
            
                <a href="../chapter6/part3.html">
            
                    
                    内核线程初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter6/part4.html">
            
                <a href="../chapter6/part4.html">
            
                    
                    [内核线程创建与切换测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter6/part5.html">
            
                <a href="../chapter6/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter7/introduction.html">
            
                <a href="../chapter7/introduction.html">
            
                    
                    第七章：线程调度
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter7/part1.html">
            
                <a href="../chapter7/part1.html">
            
                    
                    线程池与线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter7/part2.html">
            
                <a href="../chapter7/part2.html">
            
                    
                    内核调度线程 idle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter7/part3.html">
            
                <a href="../chapter7/part3.html">
            
                    
                    线程调度之 Round Robin 算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter7/part4.html">
            
                <a href="../chapter7/part4.html">
            
                    
                    [线程调度测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter7/part5.html">
            
                <a href="../chapter7/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter8/introduction.html">
            
                <a href="../chapter8/introduction.html">
            
                    
                    第八章：进程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter8/part1.html">
            
                <a href="../chapter8/part1.html">
            
                    
                    [编写用户程序]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter8/part1_1.html">
            
                <a href="../chapter8/part1_1.html">
            
                    
                    合并内核与应用程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter8/part2.html">
            
                <a href="../chapter8/part2.html">
            
                    
                    在内核中实现系统调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter8/part3.html">
            
                <a href="../chapter8/part3.html">
            
                    
                    创建虚拟内存空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../chapter8/part4.html">
            
                <a href="../chapter8/part4.html">
            
                    
                    [创建进程]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../chapter8/part5.html">
            
                <a href="../chapter8/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter9/introduction.html">
            
                <a href="../chapter9/introduction.html">
            
                    
                    第九章：文件系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter9/part1.html">
            
                <a href="../chapter9/part1.html">
            
                    
                    [使用文件系统]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter9/part2.html">
            
                <a href="../chapter9/part2.html">
            
                    
                    [实现记事本]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter9/part3.html">
            
                <a href="../chapter9/part3.html">
            
                    
                    [实现终端]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter9/part4.html">
            
                <a href="../chapter9/part4.html">
            
                    
                    [文件读写]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter9/part5.html">
            
                <a href="../chapter9/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter10/introduction.html">
            
                <a href="../chapter10/introduction.html">
            
                    
                    第十章：同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="introduction.html">
            
                <a href="introduction.html">
            
                    
                    练习题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="part1.html">
            
                <a href="part1.html">
            
                    
                    1. 中断异常
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="part2.html">
            
                <a href="part2.html">
            
                    
                    2. 物理内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="part3.html">
            
                <a href="part3.html">
            
                    
                    3. 虚拟内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="part4.html">
            
                <a href="part4.html">
            
                    
                    4. 线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.5" data-path="part5.html">
            
                <a href="part5.html">
            
                    
                    5. 用户进程（+ 虚拟内存管理 + 线程管理）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.6" data-path="part6.html">
            
                <a href="part6.html">
            
                    
                    6. CPU 调度
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.13.7" data-path="part7.html">
            
                <a href="part7.html">
            
                    
                    7. 同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.8" data-path="part8.html">
            
                <a href="part8.html">
            
                    
                    8. 文件系统
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../appendix/introduction.html">
            
                <a href="../appendix/introduction.html">
            
                    
                    附录
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../appendix/inline_asm.html">
            
                <a href="../appendix/inline_asm.html">
            
                    
                    内联汇编
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../appendix/install_rust.html">
            
                <a href="../appendix/install_rust.html">
            
                    
                    安装 rust
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >7. 同步互斥</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="7-&#x540C;&#x6B65;&#x4E92;&#x65A5;">7. &#x540C;&#x6B65;&#x4E92;&#x65A5;</h1>
<h2 id="&#x5B9E;&#x9A8C;&#x8981;&#x6C42;">&#x5B9E;&#x9A8C;&#x8981;&#x6C42;</h2>
<blockquote>
<p>&#x672C;&#x7AE0;&#x7F16;&#x7A0B;&#x95EE;&#x9898;&#x8F83;&#x7B80;&#x5355;&#xFF0C;&#x4E3B;&#x8981;&#x96BE;&#x5EA6;&#x5728;&#x9605;&#x8BFB;&#x4EE3;&#x7801;&#x4E0E;&#x95EE;&#x7B54;&#x9898;</p>
</blockquote>
<ol>
<li>&#x7F16;&#x7A0B;&#xFF1A;&#x5C06;&#x5728;&#x5B9E;&#x9A8C;&#x6307;&#x5BFC;&#x4E2D;&#x63D0;&#x4F9B; <code>Mutex</code> &#x7684;&#x5B9E;&#x73B0;&#x6846;&#x67B6;&#x3001;<code>sleep</code> &#x7684;&#x5B9E;&#x73B0;&#x3001;<code>spawn</code> &#x7684;&#x5B9E;&#x73B0;&#x548C;&#x54F2;&#x5B66;&#x5BB6;&#x5C31;&#x9910;&#x95EE;&#x9898;&#x6D4B;&#x8BD5;&#xFF0C;&#x8BF7;&#x5C06;&#x5B83;&#x4EEC;&#x590D;&#x5236;&#x5230;&#x4F60;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x5E76;&#x5B8C;&#x6210; <code>Mutex</code> &#x4E2D;&#x7684; <code>TODO</code> &#x90E8;&#x5206;&#x3002;&#xFF08;8 &#x5206;&#xFF09;</li>
<li>&#x56DE;&#x7B54;&#xFF1A;<code>Mutex</code> &#x7684;&#x5B9E;&#x73B0;&#x4E2D;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x5F15;&#x5165; <code>MutexGuard</code> &#xFF1F;&#xFF08;3 &#x5206;&#xFF09;</li>
<li>&#x56DE;&#x7B54;&#xFF1A;<code>Mutex</code> &#x7684;&#x5B9E;&#x73B0;&#x4E2D;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x4FEE;&#x6539; <code>yield_now</code> &#x5E76;&#x589E;&#x52A0; <code>park</code> &#xFF0C;&#x5982;&#x679C;&#x90FD;&#x4ECD;&#x7136;&#x4F7F;&#x7528;&#x65E7;&#x7684; <code>yield_now</code> &#x4F1A;&#x51FA;&#x73B0;&#x4EC0;&#x4E48;&#x95EE;&#x9898;&#xFF1F;&#xFF08;3 &#x5206;&#xFF09;</li>
<li>&#x56DE;&#x7B54;&#xFF1A;<code>sleep</code> &#x7684;&#x5B9E;&#x73B0;&#x4E2D;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x5728; <code>idle_main</code> &#x4E2D;&#x589E;&#x52A0;&#x4E00;&#x77AC;&#x95F4;&#x7684;&#x4E2D;&#x65AD;&#x5F00;&#x542F;&#xFF0C;&#x4E0D;&#x589E;&#x52A0;&#x8FD9;&#x90E8;&#x5206;&#x4F1A;&#x51FA;&#x73B0;&#x4EC0;&#x4E48;&#x95EE;&#x9898;&#xFF1F;&#xFF08;3 &#x5206;&#xFF09;</li>
<li>&#x56DE;&#x7B54;&#xFF1A;&#x5728;&#x54F2;&#x5B66;&#x5BB6;&#x5C31;&#x9910;&#x6D4B;&#x8BD5;&#x4E2D;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x4FEE;&#x6539; <code>spie</code> &#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#x53EF;&#x80FD;&#x4F1A;&#x51FA;&#x73B0;&#x4EC0;&#x4E48;&#x95EE;&#x9898;&#xFF1F;&#xFF08;3 &#x5206;&#xFF09;</li>
</ol>
<p>&#x4E3A;&#x4E86;&#x8BA9;&#x505A;&#x9898;&#x4F53;&#x9A8C;&#x66F4;&#x597D;&#xFF0C;&#x6211;&#x4F1A;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x540E;&#x518D;&#x590D;&#x5236;&#x4E00;&#x904D;&#x4E0A;&#x9762;&#x7684;&#x9898;&#x76EE;&#x3002;</p>
<h2 id="&#x5B9E;&#x9A8C;&#x6307;&#x5BFC;">&#x5B9E;&#x9A8C;&#x6307;&#x5BFC;</h2>
<h3 id="mutex-&#x6846;&#x67B6;"><code>Mutex</code> &#x6846;&#x67B6;</h3>
<p>&#x4E92;&#x65A5;&#x9501;&#xFF08;Mutex&#xFF09;&#xFF0C;&#x7528;&#x4E8E;&#x4FDD;&#x62A4;&#x8D44;&#x6E90;&#x4E0D;&#x4F1A;&#x88AB;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x64CD;&#x4F5C;&#x3002;</p>
<ul>
<li><code>sync/mod.rs</code></li>
</ul>
<pre class="language-"><code class="lang-rust"><span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">condvar</span><span class="token punctuation">;</span>
<span class="token keyword">pub</span> <span class="token keyword">use</span> <span class="token keyword">self</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>mutex<span class="token punctuation">::</span></span><span class="token punctuation">{</span><span class="token class-name">Mutex</span> <span class="token keyword">as</span> <span class="token class-name">SleepLock</span><span class="token punctuation">,</span> <span class="token class-name">MutexGuard</span> <span class="token keyword">as</span> <span class="token class-name">SleepLockGuard</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">mod</span> <span class="token module-declaration namespace">mutex</span><span class="token punctuation">;</span>
</code></pre>
<ul>
<li>&#x521B;&#x5EFA; <code>sync/mutex.rs</code></li>
</ul>
<p>&#x8FD9;&#x91CC;&#x7ED9;&#x51FA;&#x4E86; <code>rust std</code> &#x5E93;&#x4E2D; Mutext &#x5B9E;&#x73B0;&#x7684;&#x9B54;&#x6539;&#x7248;&#xFF0C;&#x4F60;&#x53EA;&#x9700;&#x8981;&#x5B8C;&#x6210;&#x4E24;&#x5904;&#xFF08;&#x5199;&#x4E86; <code>TODO</code> &#x7684;&#x5730;&#x65B9;&#xFF09;&#x7B80;&#x5355;&#x7684;&#x586B;&#x7A7A;&#x5373;&#x53EF;&#x3002;</p>
<p>&#x5F53; <code>Mutex<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>T</span><span class="token punctuation">&gt;</span></span></code> &#x88AB;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5360;&#x7528;&#x65F6;&#xFF0C;&#x5176;&#x5B83;&#x8BD5;&#x56FE;&#x8BBF;&#x95EE;&#x8BE5;&#x7EBF;&#x7A0B;&#x7684;&#x7EBF;&#x7A0B;&#x4F1A;&#x4E3B;&#x52A8;&#x8BA9;&#x51FA;&#x81EA;&#x5DF1;&#x7684;&#x65F6;&#x95F4;&#x7247;&#xFF0C;&#x7ED9;&#x5176;&#x5B83;&#x7EBF;&#x7A0B;&#x8C03;&#x5EA6;&#x3002;</p>
<p>&#x800C;&#x5F53;&#x8D44;&#x6E90;&#x4F7F;&#x7528;&#x5B8C;&#x6BD5;&#x65F6;&#xFF0C;&#x5E94;&#x8BE5;&#x80FD;&#x591F;&#x81EA;&#x52A8;&#x91CA;&#x653E;&#x8D44;&#x6E90;&#x3002;</p>
<p>&#x4E92;&#x65A5;&#x9501;&#x6846;&#x67B6;&#x4EE3;&#x7801;&#xFF1A;<a href="https://github.com/rcore-os/rCore_tutorial_doc/tree/master/exercise/code/mutex.rs" target="_blank">mutex.rs</a></p>
<blockquote>
<p>&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x5F15;&#x5165; <code>MutexGuard</code> &#xFF1F;&#xFF08;3 &#x5206;&#xFF09;</p>
</blockquote>
<ul>
<li>&#x4FEE;&#x6539; <code>yield_now</code></li>
</ul>
<pre class="language-"><code class="lang-rust"><span class="token comment">// in process/processor.rs</span>

<span class="token keyword">impl</span> <span class="token class-name">Processor</span> <span class="token punctuation">{</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">yield_now</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> inner <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">inner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token operator">!</span>inner<span class="token punctuation">.</span>current<span class="token punctuation">.</span><span class="token function">is_none</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
                <span class="token keyword">let</span> flags <span class="token operator">=</span> <span class="token function">disable_and_store</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">let</span> current_thread <span class="token operator">=</span> <span class="token operator">&amp;</span><span class="token keyword">mut</span> inner<span class="token punctuation">.</span>current<span class="token punctuation">.</span><span class="token function">as_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token number">1</span><span class="token punctuation">;</span>
                current_thread<span class="token punctuation">.</span><span class="token function">switch_to</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token operator">*</span>inner<span class="token punctuation">.</span>idle<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token function">restore</span><span class="token punctuation">(</span>flags<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>&#x5B9E;&#x73B0; <code>park</code></li>
</ul>
<pre class="language-"><code class="lang-rust"><span class="token comment">// in process/mod.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">park</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token constant">CPU</span><span class="token punctuation">.</span><span class="token function">park</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>


<span class="token keyword">impl</span> <span class="token class-name">Processor</span> <span class="token punctuation">{</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">park</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">inner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>pool<span class="token punctuation">.</span><span class="token function">set_sleep</span><span class="token punctuation">(</span><span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">current_tid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">yield_now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// in process/thread_pool.rs</span>

<span class="token keyword">impl</span> <span class="token class-name">ThreadPool</span> <span class="token punctuation">{</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">set_sleep</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token keyword">self</span><span class="token punctuation">,</span> tid<span class="token punctuation">:</span> <span class="token class-name">Tid</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> proc <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span>threads<span class="token punctuation">[</span>tid<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">as_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">&quot;thread not exist&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        proc<span class="token punctuation">.</span>status <span class="token operator">=</span> <span class="token class-name">Status</span><span class="token punctuation">::</span><span class="token class-name">Sleeping</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>&#x5C06;&#x4E4B;&#x524D;&#x6240;&#x6709;&#x7528;&#x5230; <code>yield_now</code> &#x7684;&#x5730;&#x65B9;&#xFF08;&#x9664;&#x4E86; <code>Mutex.rs</code>&#xFF09;&#x66FF;&#x6362;&#x4E3A; <code>park</code></li>
</ul>
<pre class="language-"><code class="lang-rust"><span class="token comment">// in sync/condvar.rs</span>

<span class="token keyword">impl</span> <span class="token class-name">Condvar</span> <span class="token punctuation">{</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">wait</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">self</span><span class="token punctuation">.</span>wait_queue<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token function">current_tid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">park</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// syscall.rs</span>

<span class="token keyword">fn</span> <span class="token function-definition function">sys_exec</span><span class="token punctuation">(</span>path<span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">isize</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> valid <span class="token operator">=</span> <span class="token namespace">process<span class="token punctuation">::</span></span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">unsafe</span> <span class="token punctuation">{</span> <span class="token function">from_cstr</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token class-name">Some</span><span class="token punctuation">(</span><span class="token namespace">process<span class="token punctuation">::</span></span><span class="token function">current_tid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> valid <span class="token punctuation">{</span>
        <span class="token namespace">process<span class="token punctuation">::</span></span><span class="token function">park</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x4FEE;&#x6539; <code>yield_now</code> &#x5E76;&#x589E;&#x52A0; <code>park</code> &#xFF0C;&#x5982;&#x679C;&#x90FD;&#x4ECD;&#x7136;&#x4F7F;&#x7528;&#x65E7;&#x7684; <code>yield_now</code> &#x4F1A;&#x51FA;&#x73B0;&#x4EC0;&#x4E48;&#x95EE;&#x9898;&#xFF1F;&#xFF08;3 &#x5206;&#xFF09;</p>
</blockquote>
<h3 id="&#x5B9E;&#x73B0;-sleep">&#x5B9E;&#x73B0; <code>sleep</code></h3>
<ul>
<li>&#x589E;&#x52A0;&#x8BA1;&#x65F6;&#x5668; <code>process/timer.rs</code></li>
</ul>
<p>&#x8BA1;&#x65F6;&#x5668;&#x4EE3;&#x7801;&#xFF1A;<a href="https://github.com/rcore-os/rCore_tutorial_doc/tree/master/exercise/code/timer.rs" target="_blank">timer.rs</a></p>
<p>&#x5230;&#x65F6;&#x95F4;&#x540E;&#x4F1A;&#x89E6;&#x53D1;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x5BF9;&#x4E8E;&#x540E;&#x9762; <code>sleep</code> &#x7684;&#x7528;&#x6CD5;&#xFF0C;&#x5728;&#x7EBF;&#x7A0B;&#x7761;&#x7720;&#x65F6;&#x95F4;&#x5230;&#x4E86;&#x7684;&#x65F6;&#x5019;&#x5C06;&#x5176;&#x5524;&#x9192;&#xFF08;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x8BBE;&#x7F6E;&#x4E3A; <code>wakeup(tid)</code> &#xFF09;&#x3002;</p>
<ul>
<li>&#x589E;&#x52A0; <code>sleep</code></li>
</ul>
<pre class="language-"><code class="lang-rust"><span class="token comment">// timer.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">u64</span> <span class="token punctuation">{</span>
    <span class="token function">get_cycle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token constant">TIMEBASE</span>
<span class="token punctuation">}</span>

<span class="token comment">// process/mod.rs</span>

<span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">timer</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token keyword">self</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>timer<span class="token punctuation">::</span></span><span class="token class-name">Timer</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>timer<span class="token punctuation">::</span></span>now<span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">lazy_static<span class="token punctuation">::</span></span>lazy_static<span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">spin<span class="token punctuation">::</span></span><span class="token class-name">Mutex</span><span class="token punctuation">;</span>

<span class="token macro property">lazy_static!</span> <span class="token punctuation">{</span>
    <span class="token keyword">static</span> <span class="token keyword">ref</span> <span class="token constant">TIMER</span><span class="token punctuation">:</span> <span class="token class-name">Mutex</span><span class="token operator">&lt;</span><span class="token class-name">Timer</span><span class="token operator">&gt;</span> <span class="token operator">=</span> <span class="token class-name">Mutex</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token class-name">Timer</span><span class="token punctuation">::</span><span class="token function">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">tick</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token constant">CPU</span><span class="token punctuation">.</span><span class="token function">tick</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token constant">TIMER</span><span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">tick</span><span class="token punctuation">(</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">sleep</span><span class="token punctuation">(</span>sec<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> tid <span class="token operator">=</span> <span class="token function">current_tid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token constant">TIMER</span>
        <span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token punctuation">(</span>sec <span class="token operator">*</span> <span class="token number">100</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">u64</span><span class="token punctuation">,</span> <span class="token keyword">move</span> <span class="token closure-params"><span class="token closure-punctuation punctuation">|</span><span class="token closure-punctuation punctuation">|</span></span> <span class="token function">wake_up</span><span class="token punctuation">(</span>tid<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">park</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li>&#x4FEE;&#x6539; <code>idle</code></li>
</ul>
<pre class="language-"><code class="lang-rust"><span class="token comment">// in interrupt.rs</span>

<span class="token attribute attr-name">#[inline(always)]</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">enable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
        <span class="token macro property">asm!</span><span class="token punctuation">(</span><span class="token string">&quot;csrsi sstatus, 1 &lt;&lt; 1&quot;</span> <span class="token punctuation">::</span><span class="token punctuation">::</span> <span class="token string">&quot;volatile&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// in process/processor.rs</span>

<span class="token keyword">impl</span> <span class="token class-name">Processor</span> <span class="token punctuation">{</span>
    <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">idle_main</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">!</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> inner <span class="token operator">=</span> <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">inner</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">disable_and_store</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">loop</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token keyword">let</span> <span class="token class-name">Some</span><span class="token punctuation">(</span>thread<span class="token punctuation">)</span> <span class="token operator">=</span> inner<span class="token punctuation">.</span>pool<span class="token punctuation">.</span><span class="token function">acquire</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                inner<span class="token punctuation">.</span>current <span class="token operator">=</span> <span class="token class-name">Some</span><span class="token punctuation">(</span>thread<span class="token punctuation">)</span><span class="token punctuation">;</span>
                inner
                    <span class="token punctuation">.</span>idle
                    <span class="token punctuation">.</span><span class="token function">switch_to</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> <span class="token operator">*</span>inner<span class="token punctuation">.</span>current<span class="token punctuation">.</span><span class="token function">as_mut</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">let</span> <span class="token punctuation">(</span>tid<span class="token punctuation">,</span> thread<span class="token punctuation">)</span> <span class="token operator">=</span> inner<span class="token punctuation">.</span>current<span class="token punctuation">.</span><span class="token function">take</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                inner<span class="token punctuation">.</span>pool<span class="token punctuation">.</span><span class="token function">retrieve</span><span class="token punctuation">(</span>tid<span class="token punctuation">,</span> thread<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token function">enable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token function">disable_and_store</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                <span class="token function">enable_and_wfi</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token function">disable_and_store</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x5728; <code>idle_main</code> &#x4E2D;&#x589E;&#x52A0;&#x4E00;&#x77AC;&#x95F4;&#x7684;&#x4E2D;&#x65AD;&#x5F00;&#x542F;&#xFF0C;&#x4E0D;&#x589E;&#x52A0;&#x8FD9;&#x90E8;&#x5206;&#x4F1A;&#x51FA;&#x73B0;&#x4EC0;&#x4E48;&#x95EE;&#x9898;&#xFF1F;&#xFF08;3 &#x5206;&#xFF09;</p>
</blockquote>
<h3 id="&#x5B9E;&#x73B0;-spawn">&#x5B9E;&#x73B0; <code>spawn</code></h3>
<p>&#x7528;&#x4E8E;&#x901A;&#x8FC7;&#x51FD;&#x6570;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5185;&#x6838;&#x7EBF;&#x7A0B;&#x3002;</p>
<ul>
<li><code>process/mod.rs</code></li>
</ul>
<pre class="language-"><code class="lang-rust"><span class="token comment">/// Spawn a new kernel thread from function `f`.</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">spawn</span><span class="token operator">&lt;</span><span class="token class-name">F</span><span class="token operator">&gt;</span><span class="token punctuation">(</span>f<span class="token punctuation">:</span> <span class="token class-name">F</span><span class="token punctuation">)</span>
<span class="token keyword">where</span>
    <span class="token class-name">F</span><span class="token punctuation">:</span> <span class="token class-name">FnOnce</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token class-name">Send</span> <span class="token operator">+</span> <span class="token lifetime-annotation symbol">&apos;static</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
    <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token class-name">Box</span><span class="token punctuation">::</span><span class="token function">into_raw</span><span class="token punctuation">(</span><span class="token class-name">Box</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> new_thread <span class="token operator">=</span> <span class="token class-name">Thread</span><span class="token punctuation">::</span><span class="token function">new_kernel</span><span class="token punctuation">(</span><span class="token function">entry</span><span class="token punctuation">::</span><span class="token operator">&lt;</span><span class="token class-name">F</span><span class="token operator">&gt;</span> <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    new_thread<span class="token punctuation">.</span><span class="token function">append_initial_arguments</span><span class="token punctuation">(</span><span class="token punctuation">[</span>f <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token constant">CPU</span><span class="token punctuation">.</span><span class="token function">add_thread</span><span class="token punctuation">(</span>new_thread<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// define a normal function, pass the function object from argument</span>
    <span class="token keyword">extern</span> <span class="token string">&quot;C&quot;</span> <span class="token keyword">fn</span> <span class="token function-definition function">entry</span><span class="token operator">&lt;</span><span class="token class-name">F</span><span class="token operator">&gt;</span><span class="token punctuation">(</span>f<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">!</span>
    <span class="token keyword">where</span>
        <span class="token class-name">F</span><span class="token punctuation">:</span> <span class="token class-name">FnOnce</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token class-name">Send</span> <span class="token operator">+</span> <span class="token lifetime-annotation symbol">&apos;static</span><span class="token punctuation">,</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token keyword">unsafe</span> <span class="token punctuation">{</span> <span class="token class-name">Box</span><span class="token punctuation">::</span><span class="token function">from_raw</span><span class="token punctuation">(</span>f <span class="token keyword">as</span> <span class="token operator">*</span><span class="token keyword">mut</span> <span class="token class-name">F</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
        <span class="token function">f</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token macro property">unreachable!</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x54F2;&#x5B66;&#x5BB6;&#x5C31;&#x9910;&#x95EE;&#x9898;&#x6D4B;&#x8BD5;">&#x54F2;&#x5B66;&#x5BB6;&#x5C31;&#x9910;&#x95EE;&#x9898;&#x6D4B;&#x8BD5;</h3>
<p><a href="https://github.com/rcore-os/rCore_tutorial/blob/master/test/mutex_test.rs" target="_blank">&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;</a></p>
<p>&#x5C06;&#x8BE5;&#x6587;&#x4EF6;&#x76F4;&#x63A5;&#x66FF;&#x6362; <code>init.rs</code> &#xFF0C;&#x6CA1;&#x6709;&#x54F2;&#x5B66;&#x5BB6;&#x62FF;&#x9519;&#x53C9;&#x5B50;&#x4E14;&#x4E94;&#x4E2A;&#x5185;&#x6838;&#x7EBF;&#x7A0B;&#x5747;&#x6B63;&#x5E38;&#x9000;&#x51FA;&#x5219;&#x8868;&#x793A;&#x6D4B;&#x8BD5;&#x901A;&#x8FC7;&#x3002;</p>
<ul>
<li>&#x4FEE;&#x6539; <code>spie</code></li>
</ul>
<pre class="language-"><code class="lang-rust"><span class="token keyword">impl</span> <span class="token class-name">ContextContent</span> <span class="token punctuation">{</span>
    <span class="token keyword">fn</span> <span class="token function-definition function">new_kernel_thread</span><span class="token punctuation">(</span>entry<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> kstack_top<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> satp<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">ContextContent</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> <span class="token keyword">mut</span> content <span class="token operator">=</span> <span class="token class-name">ContextContent</span> <span class="token punctuation">{</span>
            ra<span class="token punctuation">:</span> __trapret <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
            satp<span class="token punctuation">,</span>
            s<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token number">12</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            tf<span class="token punctuation">:</span> <span class="token punctuation">{</span>
                <span class="token keyword">let</span> <span class="token keyword">mut</span> tf<span class="token punctuation">:</span> <span class="token class-name">TrapFrame</span> <span class="token operator">=</span> <span class="token keyword">unsafe</span> <span class="token punctuation">{</span> <span class="token function">zeroed</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
                tf<span class="token punctuation">.</span>x<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> kstack_top<span class="token punctuation">;</span>
                tf<span class="token punctuation">.</span>sepc <span class="token operator">=</span> entry<span class="token punctuation">;</span>
                tf<span class="token punctuation">.</span>sstatus <span class="token operator">=</span> <span class="token namespace">sstatus<span class="token punctuation">::</span></span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                tf<span class="token punctuation">.</span>sstatus<span class="token punctuation">.</span><span class="token function">set_spp</span><span class="token punctuation">(</span><span class="token namespace">sstatus<span class="token punctuation">::</span></span><span class="token constant">SPP</span><span class="token punctuation">::</span><span class="token class-name">Supervisor</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                tf<span class="token punctuation">.</span>sstatus<span class="token punctuation">.</span><span class="token function">set_spie</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// changed</span>
                tf<span class="token punctuation">.</span>sstatus<span class="token punctuation">.</span><span class="token function">set_sie</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                tf
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span><span class="token punctuation">;</span>
        content
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x4E3A;&#x4EC0;&#x4E48;&#x9700;&#x8981;&#x4FEE;&#x6539; <code>spie</code> &#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#x53EF;&#x80FD;&#x4F1A;&#x51FA;&#x73B0;&#x4EC0;&#x4E48;&#x95EE;&#x9898;&#xFF1F;&#xFF08;3 &#x5206;&#xFF09;</p>
</blockquote>
<p><div id="page-footer" class="localized-footer"><hr><p><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css"></p>
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
  var gitalk = new Gitalk({
    clientID: "ab871063a9fa7f1da571",
    clientSecret: "4760b384e3e452d2241d859cfa6a7da21a8729ec",
    repo: "rCore_tutorial_doc",
    owner: "rcore-os",
    admin: [
      "chyyuu",
      "xyongcn",
      "wangrunji0408",
      "xy-plus",
      "PanQL",
      "wyfcyx",
      "equation314"
    ],
    id: location.pathname,
    distractionFreeMode: false
  });
  gitalk.render("gitalk-container");
</script>
</div></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="part6.html" class="navigation navigation-prev " aria-label="Previous page: 6. CPU 调度">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="part8.html" class="navigation navigation-next " aria-label="Next page: 8. 文件系统">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"7. 同步互斥","level":"1.13.7","depth":2,"next":{"title":"8. 文件系统","level":"1.13.8","depth":2,"path":"exercise/part8.md","ref":"./exercise/part8.md","articles":[]},"previous":{"title":"6. CPU 调度","level":"1.13.6","depth":2,"path":"exercise/part6.md","ref":"./exercise/part6.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","-highlight","prism","localized-footer","mermaid-gb3"],"pluginsConfig":{"chapter-fold":{},"prism":{"css":["prismjs/themes/prism-tomorrow.css"]},"emphasize":{},"search":{},"localized-footer":{"filename":"extensions/comment/gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"hide-element":{"elements":[".gitbook-link"]},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"mermaid-gb3":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"exercise/part7.md","mtime":"2020-09-09T11:31:57.035Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-10T06:57:01.716Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

